]> git.r.bdr.sh - rbdr/super-polarity/blobdiff - Super Polarity/Actors/StandardShip.cs
Implements polarity system
[rbdr/super-polarity] / Super Polarity / Actors / StandardShip.cs
diff --git a/Super Polarity/Actors/StandardShip.cs b/Super Polarity/Actors/StandardShip.cs
new file mode 100644 (file)
index 0000000..50a671a
--- /dev/null
@@ -0,0 +1,114 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework.Content;
+using System.Security.Cryptography; 
+
+namespace SuperPolarity
+{
+    class StandardShip : Ship
+    {
+
+        protected int ChangeRate;
+        protected int CurrentTime;
+        protected int AngleChangeProbability;
+        protected int BouncePadding;
+        protected float RotationFactor;
+        protected Random Random;
+        protected bool AddingAngle;
+
+        public StandardShip(Game newGame) : base(newGame) {}
+
+        public override void  Initialize(Texture2D texture, Vector2 position)
+        {
+            base.Initialize(texture, position);
+
+            var cryptoResult = new byte[4];
+            new RNGCryptoServiceProvider().GetBytes(cryptoResult);
+
+            ChangeRate = 50;
+            AngleChangeProbability = 50;
+            BouncePadding = 0;
+            MaxVelocity = 1;
+            CurrentTime = 0;
+            RotationFactor = (float) (3 * Math.PI / 180);
+            Random = new Random(BitConverter.ToInt32(cryptoResult, 0));
+            AddingAngle = true;
+        }
+
+        public override void Magnetize(Ship ship, float distance, float angle)
+        {
+            if (ship.GetType() == typeof(MainShip)) {
+                base.Magnetize(ship, distance, angle);
+            }
+        }
+
+        public override void Update(GameTime gameTime)
+        {
+            CurrentTime += gameTime.ElapsedGameTime.Milliseconds;
+            if (!Magnetizing)
+            {
+                AutoMove();
+                BounceBack();
+            }
+            ChangeAngle();
+            Position += Velocity;
+            Magnetizing = false;
+        }
+
+        protected void AutoMove()
+        {
+            float newAngle = Angle;
+
+            if (CurrentTime < ChangeRate)
+            {
+                return;
+            }
+
+            if (Random.Next(AngleChangeProbability) == 2)
+            {
+                AddingAngle = !AddingAngle;
+            }
+
+            CurrentTime = 0;
+
+            if (AddingAngle)
+            {
+                newAngle += (float) ( Random.NextDouble() * RotationFactor);
+            }
+            else
+            {
+                newAngle -= (float) (Random.NextDouble() * RotationFactor);
+            }
+
+            Velocity.X = (float) (MaxVelocity * Math.Cos(newAngle));
+            Velocity.Y = (float) (MaxVelocity * Math.Sin(newAngle));
+        }
+
+        protected void BounceBack()
+        {
+            if (Position.X + Width < -BouncePadding && Velocity.X < 0)
+            {
+                Velocity.X = -Velocity.X;
+            }
+
+            if (Position.Y + Height < -BouncePadding && Velocity.Y < 0)
+            {
+                Velocity.Y = -Velocity.Y;
+            }
+
+            if (Position.X > game.GraphicsDevice.Viewport.Width +  BouncePadding && Velocity.X > 0)
+            {
+                Velocity.X = -Velocity.X;
+            }
+
+            if (Position.Y > game.GraphicsDevice.Viewport.Height + BouncePadding && Velocity.Y > 0)
+            {
+                Velocity.Y = -Velocity.Y;
+            }
+        }
+    }
+}